package com.mhl.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.mhl.campuscabinet.R;
import com.mhl.entity.Comment;


import java.util.List;

public class Comment_Adapter extends RecyclerView.Adapter<Comment_Adapter.MyViewHolder> {

    private final List<Comment> mCommentList;
    private Activity activity;
    //构造方法，传入数据
    public Comment_Adapter(List<Comment> list, Activity activity){
        this.mCommentList = list;
        this.activity = activity;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //创建ViewHolder，返回每一项的布局
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_comment, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        //将数据和控件绑定
//        holder.textView.setText(list.get(position));
        Comment comment = mCommentList.get(position);

        holder.commentimage.setImageResource(comment.getImageId());
        //正式的获取图片是这个
        //holder.commentimage.setImageBitmap(HttpUtil.getHttpBitmap(comment.getURL()));
        Glide.with(activity).load("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fuserimg" +
                ".yingyonghui.com%2Fhead%2F94%2F1486907501327%2F6212294.png-thumb&refer=http%3A%2F%2Fuserimg" +
                ".yingyonghui.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1643192025&t=edb5ea280000f48a78165c1dc5176fbd")
                .into(holder.commentimage);
        holder.commentname.setText(comment.getName());
        holder.commenttime.setText(comment.getTime());
        holder.commentcontent.setText(comment.getContent());

    }

    @Override
    public int getItemCount() {
        //返回Item总条数
        return mCommentList.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder{
        ImageView commentimage;
        TextView commentname;
        TextView commenttime;
        TextView commentcontent;
        public MyViewHolder(View itemView) {
            super(itemView);
            commentimage = (ImageView) itemView.findViewById(R.id.comment_image);
            commentname = itemView.findViewById(R.id.comment_name);
            commenttime = itemView.findViewById(R.id.comment_time);
            commentcontent = itemView.findViewById(R.id.comment_content);
        }
    }
}

